package com.clinic.logic.prescription;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.clinic.accessor.db.SyptomAccessor;
import com.clinic.entity.db.Syptom;
import com.clinic.entity.gui.GuiSyptom;
import com.clinic.entity.param.Index;
import com.clinic.util.ConvertUtil;

@Component
public class SyptomLogic {

	@Autowired
	private SyptomAccessor syptomAccessor;
	
	public GuiSyptom getSyptomByName(String name) {
		Syptom syptom = syptomAccessor.getSyptomByName(name);
		return ConvertUtil.getGuiSyptomBySyptom(syptom);
	}
	
	public GuiSyptom insertSyptom(String name) {
		Syptom syptom = new Syptom();
		syptom.setName(name);
		Syptom mSyptom = syptomAccessor.insertOrUpdate(syptom);
		return ConvertUtil.getGuiSyptomBySyptom(mSyptom);
	}
	
	public List<GuiSyptom> getSyptomList(Index index) {
		List<Syptom> syptomList = syptomAccessor.getSyptoms(index);
		Set<String> names = new HashSet<>();
		List<GuiSyptom> result = new ArrayList<GuiSyptom>();
		for (Syptom syptom : syptomList) {
			if (!names.contains(syptom.getName())) {
				names.add(syptom.getName()); // keep symptom name in a collection to avoid duplicate entry
				GuiSyptom guiSyptom = ConvertUtil.getGuiSyptomBySyptom(syptom);
				if (guiSyptom.isAvailable()) {
					result.add(guiSyptom);
				}
			}
		}
		return result;
	}
	
}
